package com.example.cadmin.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cadmin.entity.AccessLogs;
import com.example.cadmin.entity.BorrowingRecords;
import com.example.cadmin.entity.FileLog;
import com.example.cadmin.mapper.AccessLogsMapper;
import com.example.cadmin.mapper.BorrowingRecordsMapper;
import com.example.cadmin.mapper.FileLogMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zzsoft
 * @since 2023-01-09
 */
@Service
public class FileLogService extends ServiceImpl<FileLogMapper, FileLog> {

    @Resource
    public FileLogMapper fileLogMapper;

    @Resource
    public AccessLogsMapper accessLogsMapper;
    @Resource
    public BorrowingRecordsMapper borrowingRecordsMapper;

    /**
     * 档案操作管理list接口
     *
     * @param page
     * @param limit
     */
    public Map<String, Object> fileLogList(int page, int limit, String startDate, String endDate, String fileName,String borrowingType, String number) {
        IPage<Map<String, Object>> borrowingPage = new Page<>(page, limit);//参数一是当前页，参数二是每页个数
        QueryWrapper<BorrowingRecords> borrowingRecordsQueryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(fileName)) {
            borrowingRecordsQueryWrapper.like("file_name", fileName);
        }
        if (StrUtil.isNotEmpty(number)) {
            borrowingRecordsQueryWrapper.eq("number", number);
        }
        if (StrUtil.isNotEmpty(startDate) && StrUtil.isNotEmpty(endDate)) {
            // 时间偏于一天
            Date date = DateUtil.parse(endDate, "yyyy-MM-dd");
            Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 1);
            String formatDate = DateUtil.formatDate(newDate);
            borrowingRecordsQueryWrapper.ge("create_time", startDate).le("create_time", formatDate);
        }
        if (StrUtil.isNotEmpty(borrowingType)) {
            borrowingRecordsQueryWrapper.eq("borrowing_type", borrowingType);
        }
        borrowingRecordsQueryWrapper.orderByDesc("create_time");
        borrowingPage = borrowingRecordsMapper.selectMapsPage(borrowingPage, borrowingRecordsQueryWrapper);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", 0);
        resMap.put("msg", "数据请求成功");
        resMap.put("data", borrowingPage.getRecords());
        resMap.put("count", borrowingPage.getTotal());
        return resMap;
    }

    /**
     * 清空日志
     */
    public ResponseModel clearLog() {
        UpdateWrapper<FileLog> fileLogUpdateWrapper = new UpdateWrapper<>();
        fileLogUpdateWrapper.set("status", 0);
        fileLogMapper.update(null, fileLogUpdateWrapper);
        return ResponseModel.success("日志已清空");
    }

}
